package caseManagerBusiness;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by yangxianming on 2018/7/26.
 * 123
 */
public class AddCase implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        //读取请求json
        InputStreamReader inputStreamReader;
        BufferedReader bufferedReader;
        String requestInfo = "";

        try {
            inputStreamReader = new InputStreamReader(request.getInputStream(), "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null)
                requestInfo = requestInfo + line;
        }catch (Exception e){
            //请求流如果出现异常的时候
            returnJson.setApiStatus(0);
            returnJson.setErrorMsg(e.getMessage());
            return returnJson.toString();
        }

        //数据库插入
        System.out.println(requestInfo);
        JSONObject apiJson = JSON.parseObject(requestInfo);
        //增加显示字段
        apiJson.put("isjump", "0");
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        SqlReturnModel sqlReturnModel = sqlDispatch.insert("testcase", apiJson);

        returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
        returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        return returnJson.toString();
    }
}
